﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

/*
 * Author:Conis
 * Date:
 * Description:排序的SQL语句
 */

namespace Conis.Squirrel.SqlBuilder
{
 /// <summary>
    /// 排序的抽象类
    /// </summary>
    public abstract class BaseOrderBy
    {
        private StringBuilder sb;
        private int count = 0;
        public BaseOrderBy()
        {
            sb = new StringBuilder();
        }

        #region IOrderBy 成员

        /// <summary>
        /// 添加一个排序片断
        /// </summary>
        /// <param name="fieldName">排序的字段名称</param>
        /// <param name="direction">排序的方向</param>
        /// <returns></returns>
        public virtual BaseOrderBy Append(string fieldName, Direction direction)
        {
            //多个排序条件
            if (this.count > 0)
            {
                this.sb.Append(",");
            }
            this.sb.Append(fieldName).Append(" ").Append(direction.ToString());
            this.count++;
            return this;
        }

        /// <summary>
        /// 返回生成的排序字符串
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public virtual string ToString(bool prefix)
        {
            string result = this.sb.ToString();
            if (prefix)
            {
                return " ORDER BY " + result;
            }
            else
            {
                return result;
            }
        }
        #endregion
    }
}
